package com.ruoyi.web.controller.business;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IBusinessinfoService;
import com.ruoyi.system.service.IShangjiatypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Businessinfo;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商家 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-16
 */
@Controller
@RequestMapping("/system/businessinfo")
public class BusinessinfoController extends BaseController
{
    private String prefix = "system/businessinfo";
	
	@Autowired
	private IBusinessinfoService businessinfoService;
	@Autowired
	private IShangjiatypeService shangjiatypeService;
	
	@RequiresPermissions("system:businessinfo:view")
	@GetMapping()
	public String businessinfo()
	{
	    return prefix + "/businessinfo";
	}
	
	/**
	 * 查询商家列表
	 */
	@RequiresPermissions("system:businessinfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Businessinfo businessinfo,ModelMap mmap)
	{
		startPage();
        List<Businessinfo> list = businessinfoService.selectBusinessinfoList(businessinfo);
		mmap.put("shangjias",shangjiatypeService.selectBusinesstypeList(null));
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商家列表
	 */
	@RequiresPermissions("system:businessinfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Businessinfo businessinfo)
    {
    	List<Businessinfo> list = businessinfoService.selectBusinessinfoList(businessinfo);
        ExcelUtil<Businessinfo> util = new ExcelUtil<Businessinfo>(Businessinfo.class);
        return util.exportExcel(list, "businessinfo");
    }
	
	/**
	 * 新增商家
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("shangjias",shangjiatypeService.selectBusinesstypeList(null));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商家
	 */
	@RequiresPermissions("system:businessinfo:add")
	@Log(title = "商家", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Businessinfo businessinfo)
	{
		//商家账号
		SysUser user = getSysUser();
		businessinfo.setUserId(user.getUserId());
		return toAjax(businessinfoService.insertBusinessinfo(businessinfo));
	}

	/**
	 * 修改商家
	 */
	@GetMapping("/edit/{businessinfoId}")
	public String edit(@PathVariable("businessinfoId") Integer businessinfoId, ModelMap mmap)
	{
		Businessinfo businessinfo = businessinfoService.selectBusinessinfoById(businessinfoId);
		mmap.put("shangjias",shangjiatypeService.selectBusinessByShangPinId(businessinfoId));
		mmap.put("businessinfo", businessinfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商家
	 */
	@RequiresPermissions("system:businessinfo:edit")
	@Log(title = "商家", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Businessinfo businessinfo)
	{
		//商家账号
		if(StringUtils.isEmpty(businessinfo.getUid())){
			SysUser user = getSysUser();
			businessinfo.setUserId(user.getUserId());
		}
		return toAjax(businessinfoService.updateBusinessinfo(businessinfo));
	}
	
	/**
	 * 删除商家
	 */
	@RequiresPermissions("system:businessinfo:remove")
	@Log(title = "商家", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(businessinfoService.deleteBusinessinfoByIds(ids));
	}
	/**
	 * 用户状态修改
	 */
	@Log(title = "商户管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:user:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(Businessinfo businessinfo)
	{
		return toAjax(businessinfoService.changeStatus(businessinfo));
	}
	
}
